package com.sv.ues.fia.tourismosv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import sv.ues.fia.tourismosv.R;
import sv.ues.fia.tourismosv.ws.ControladorServicio;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Toast;

public class MapaActivity extends FragmentActivity implements OnMarkerClickListener{
	
	private GoogleMap map;
	LatLng ESA;/* = new LatLng(13.683465,-89.226163);*/
	Marker marcador;
	Integer tipo;
	String busca;
	Integer todo;
	CrearBaseTurismo Helper;
	private final String urlHostingGratuito = "http://turismoapp.webuda.com/ws_buscar.php?lugar=";
	private final String urlLocal = "http://localhost:8080/WSTturismoApp/webresources/sv.ues.fia.turismo.entidad.lugar/by?nombreParam=";

	ArrayList<HashMap<String, String>> listaLugares = new ArrayList<HashMap<String, String>>();
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mapa);
		Helper = new CrearBaseTurismo(this);
		Bundle bundle = getIntent().getExtras();
		tipo = bundle.getInt("Tipo");
		busca = bundle.getString("Busca");
		todo = bundle.getInt("Todo");
		
		
		setUpMapIfNeeded();
		map.setOnMarkerClickListener(this);
		
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
		.permitAll().build();
		StrictMode.setThreadPolicy(policy);
	}
	
	/*@Override
	protected void onResume(){
		super.onResume();
		setUpMapIfNeeded();
	}*/
	
	@Override
    public boolean onMarkerClick(final Marker marker) {
        //handle click here
	 	long id = Long.valueOf(marker.getSnippet());
		Intent intent = new Intent(getApplicationContext(), ConsultarLugarActivity.class);
	    intent.putExtra("idLugar", id);
		startActivity(intent);
	        
        return true;
    }
	
	private void setUpMapIfNeeded(){
		if(map == null){
			map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
			if(map != null){
				setUpMap();
			}
		}
		
	}
	
	private void setUpMap(){
		CameraUpdate camUpd1 =CameraUpdateFactory.newLatLng(new LatLng(13.683465, -89.226163));
		map.moveCamera(camUpd1);
		map.animateCamera(CameraUpdateFactory.zoomTo(11));
		try {
			if (todo==1)
			{
				listaLugares = Helper.ubicacionLugar(tipo);
			}
			if(todo==2)
			{
				listaLugares = Helper.ubicarLugarTipo(busca,tipo);
			}
			if(todo==3)
			{
				//listaLugares = Helper.ubicarLugar(busca);
				String url = urlHostingGratuito+busca.replaceAll(" ", "%20");

				String lugaresLocales = ControladorServicio.obtenerRespuestaPeticion(url, this);
				listaLugares=ControladorServicio.buscarLugaresExterno(lugaresLocales, this);
			}
		}
		
		catch (Exception e) {
			Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
		}
		try {	
			Iterator<HashMap<String, String>> it=listaLugares.iterator();

			while (it.hasNext()){
				HashMap<String, String> punto = new HashMap<String, String>();
				punto = it.next();
				LatLng position = new LatLng(Double.valueOf((String)punto.get("lat")),Double.valueOf((String)punto.get("lng")));
				marcador = map.addMarker(new MarkerOptions()
			    .position(position)
			    .title(punto.get("titulo"))
			    .snippet(punto.get("id"))
			    .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_icon)));
				/*Toast.makeText(this, punto.get("titulo"), Toast.LENGTH_LONG).show();*/
		}
			}
			
		catch (Exception e) {
			Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
		}

		
	}

}
